mui.init(); //初始化
//获取本地信息
var my_phone = localStorage.getItem("my_phone");
var user_id = localStorage.getItem("user_id");
var mask = mui.createMask(); //遮罩层
//if(user_id == null) {
//	mui.openWindow({
//		url: "../user/login.html",
//		createNew: true
//	})
//}
var account_type; //账户类型
var level_type; //个人等级
var is_realname
var shop_name;
//查询用户信息接口
function userinfo() {
	var my_phone = localStorage.getItem("my_phone");
	
	mui.ajax(user_url + '/api/user/getuserInfo', {
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		success: function(data) {
			 console.log(JSON.stringify(data))
			if(data.code == 10000) {
				var my_role = data.resp.member_role;
				localStorage.setItem("my_leve" , my_role);
				localStorage.setItem("nikeName" , data.resp.nick_name);
				var nikeName = localStorage.getItem("nikeName");
				$('#my_business_center_user').text(nikeName);
				$('.my_user_btn').text(my_role)
				if(data.resp.finish_time != null){
					$('.my_user_time').html('有效日期:' + data.resp.finish_time)
				}
				//if(data.resp.operatorString !="" && data.resp.operatorString !=undefined){  
				//	$('#my_user_btn').text(data.resp.operatorString);
				//}else{
				//	$('#my_user_btn').text(data.resp.levelString);
				//}
				// $('#my_business_center_user').text(data.resp.user_tel);
				//account_type = data.resp.rold;
				//level_type = data.resp.level;
				//is_realname = data.resp.is_realname; //是否实名
				//var status = data.resp.status; //账户状态
				//var tmx_memberNo = data.resp.tmx_memberNo; //天美编号
				//if(tmx_memberNo) {
				//	$("#tmx_memberNo").text("会员编号：" + tmx_memberNo);
				//}
				//if(status == 2) {
				//	mui.toast('账户已被限制，请联系平台。');
				//}
			}else if( data.code == 789){
				var btnArray = ['确定', '取消'];
				mui.confirm('还未登录，请先登录', '温馨提示', btnArray, function(e) {
			        if (e.index == 1) {
			            // console.log('取消');
			        }
			        else {
			            // console.log('确定');
			          window.top.location.href = '../../html/user/login.html'
			          localStorage.clear();
			        }
			   })
			}
		},
		error: function(xhr, type, errorThrown) { //异常处理；
			console.log('接口失败');
		}
	});
}
var preDeposit; //预付款

//查询指定积分账户信息接口
function getInfo() {
	mui.ajax(integral_url + '/integral/getInfo', {
		data: {
			"phone": my_phone,
			"sign": "string",
			"user_id": user_id
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		headers: {
			'Content-Type': 'application/json'
		},
		success: function(data) {
			if(data.code == 10000) {
				var availableIntegr = data.resp.availableIntegr;
				$('#availableIntegr_num').text(availableIntegr); //可用积分
				var stockIntegr = data.resp.stockIntegr;
				$('#stockIntegr_num').text(stockIntegr); //存量积分 
				preDeposit = data.resp.preDeposit;
				$('#preDeposit_num').text(preDeposit); //预付款
			}
		},
		error: function(xhr, type, errorThrown) { //异常处理；
			console.log(type);
			var error_msg = JSON.parse(xhr.responseText)
			mui.alert(error_msg.msg)
		}
	});

}

//到公告页面
document.getElementById("my_business_center_user").addEventListener("tap", function() {
	mui.openWindow({
		url: "modify_nike_name.html",
		createNew: true
	})
});

//点击跳转页面
mui('body').on('tap', '.mui-tab-item', function() {
	window.top.location.href = this.href;
	$(this).addClass("mui-active");
});
//到公告页面
document.getElementById("notice_list").addEventListener("tap", function() {
	mui.openWindow({
		url: "notice_list.html",
		createNew: true
	})
});
//全部订单
document.getElementById("full_order").addEventListener("tap", function() {
	plus.nativeUI.showWaiting("加载中...");
	mask.show(); //显示遮罩层
	mui.openWindow({
		url: "purchase_order.html",
		id: "purchase_order",
		createNew: true,
		waiting: {
			autoShow: false
		}
	}, mask.close());
});
//可用积分
//document.getElementById("integral_detail").addEventListener("tap", function() {
//	plus.nativeUI.showWaiting("加载中...");
//	mask.show(); //显示遮罩层
//	mui.openWindow({
//		url: "../integral/integral_detail.html",
//		createNew: true,
//		waiting: {
//			autoShow: false
//		}
//	}, mask.close());
//});
//存量积分
//document.getElementById("stock_integral_detail").addEventListener("tap", function() {
//	plus.nativeUI.showWaiting("加载中...");
//	mask.show(); //显示遮罩层
//	mui.openWindow({
//		url: "../integral/stock_integral_detail.html",
//		createNew: true,
//		waiting: {
//			autoShow: false
//		}
//	}, mask.close());
//});
//预付款
//document.getElementById("advance_payment").addEventListener("tap", function() {
//	mui.openWindow({
//		url: "../integral/advance.html",
//		extras: {
//			preDeposit: preDeposit
//		},
//		createNew: true
//	});
//});
//积分中心
//$(document).on("tap", ".integral_center", function() {
//	plus.nativeUI.showWaiting("加载中...");
//	mask.show(); //显示遮罩层
//	mui.openWindow({
//		url: "../integral/integral_center.html",
//		id: "integral_center",
//		createNew: true,
//		waiting: {
//			autoShow: false
//		}
//	}, mask.close());
//});

//我的团队
//document.getElementById("my_team").addEventListener("tap", function() {
//	if(is_realname == 0) { //判断是否实名
//		mui.alert("您还未实名认证，请先实名认证", function() {
//			window.top.location.href = "../my/name_authentication.html";
//		});
//	} else {
//		mui.openWindow({
//			url: "my_team.html",
//			id: "my_team"
//		});
//	}
//
//});

//银宝中心
document.getElementById("yinbao_center").addEventListener("tap", function() {
	mui.openWindow({
		url: '../my/yinbao_center.html',
		createNew: true,
	});
});

//我的团队
document.getElementById("team-center").addEventListener("tap", function() {
	mui.openWindow({
		url: '../my/team-center.html',
		createNew: true,
	});
});


//白银中心
document.getElementById("jiaoyimingxi").addEventListener("tap", function() {
	plus.nativeUI.showWaiting("加载中...");
	mask.show(); //显示遮罩层
	mui.openWindow({
		url: "silver_list.html",
		id: "silver_list",
		createNew: true,
		waiting: {
			autoShow: false
		}
	}, mask.close());
});
//账户设置
document.getElementById("account_setting").addEventListener("tap", function() {
	plus.nativeUI.showWaiting("加载中...");
	mask.show(); //显示遮罩层
	mui.openWindow({
		url: "account_setting.html",
		id: "account_setting",
		createNew: true,
		waiting: {
			autoShow: false
		}
	}, mask.close());
});
//document.getElementById("account_set").addEventListener("tap", function() {
//	plus.nativeUI.showWaiting("加载中...");
//	mask.show(); //显示遮罩层
//	mui.openWindow({
//		url: "account_setting.html",
//		id: "account_setting",
//		createNew: true,
//		waiting: {
//			autoShow: false
//		}
//	}, mask.close());
//});
//惠民钱包
document.getElementById("jifenduihuan").addEventListener("tap", function() {
	mui.openWindow({
		url: '../my/cash-center.html',
		createNew: true,
	});
});
////运营商页面跳转
//document.getElementById("operator_center").addEventListener("tap", function() {
//	plus.nativeUI.showWaiting("加载中...");
//	mask.show(); //显示遮罩层
//	mui.openWindow({
//		url: '../Operator/operator_center.html',
//		createNew: true,
//		waiting: {
//			autoShow: false
//		}
//	}, mask.close());
//});
//银行卡
document.getElementById("bank-list").addEventListener("tap", function() {
	mui.openWindow({
		url: '../my/bank_card.html',
		createNew: true,
	});
});
//联系客服
document.getElementById("about_kefu").addEventListener("tap", function() {
	mui.openWindow({
		url: "my_online_service.html",
		id: "my_online_service"
	});
});
//旧订单
//document.getElementById("old_payment").addEventListener("tap", function() {
//	plus.nativeUI.showWaiting("加载中...");
//	mask.show(); //显示遮罩层
//	mui.openWindow({
//		url: "old_payment.html",
//		id: "old_payment",
//		createNew: true,
//		waiting: {
//			autoShow: false
//		}
//	}, mask.close());
//});
//旧订单old_integral
//document.getElementById("old_integral").addEventListener("tap", function() {
//	plus.nativeUI.showWaiting("加载中...");
//	mask.show(); //显示遮罩层
//	mui.openWindow({
//		url: "odd_integral.html",
//		id: "odd_integral",
//		createNew: true,
//		waiting: {
//			autoShow: false
//		}
//	}, mask.close());
//});

//积分分发
//document.getElementById("integral_distribution").addEventListener("tap", function() {
//	if(level_type == 1) {
//		mui.alert('成为商家才能分发');
//		return false;
//	}
//	if(is_realname == 0) { //判断是否实名
//		mui.alert("您还未实名认证，请先实名认证", function() {
//			window.top.location.href = "../my/name_authentication.html";
//		});
//	} else if(is_realname == 1) {
//		mui.openWindow({
//			url: "integral_distribution.html",
//			createNew: true
//		});
//	}
//});

//购买库存积分
//document.getElementById("buy_inventory_integral").addEventListener("tap", function() {
//	if(level_type == 1) {
//		mui.alert('成为商家才能购买');
//		return false;
//	}
//	mui.openWindow({
//		url: "../integral/buy_inventory_integral.html",
//		extras: {
//			preDeposit: preDeposit
//		},
//		createNew: true
//	});
//});

//惠民钱包接口调用
var num;
document.getElementById("merchant_entry").addEventListener("tap", function() {
	mui.ajax(user_url + '/api/Business/shopEnter',{
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒
		success:function(data) {
			console.log(JSON.stringify(data))
			if(data.code == 1){  //已交钱 尚未提交资料
				mui.openWindow({
					url: "../my/user_success.html",
					createNew: true
				});
			} else if(data.code ==-1 ){ // 未申请
				mui.openWindow({
					url: "../my/user-upgrade.html",
					createNew: true
				});
			} else if(data.code == 2 ){  // 申请中
				num = 2
				shop_name = data.resp.shop_name
				mui.openWindow({
					url: "../my/apply_wait.html",
					extras: {
						num:num,
						shop_name :shop_name
					},
					createNew: true
				});
			} else if(data.code ==3 ){  // 申请成功
				num = 3
				shop_name = data.resp.shop_name
				mui.openWindow({
					url: "../my/apply_wait.html",
					extras: {
						num:num,
						shop_name:shop_name
					},
					createNew: true
				});
			} else if(data.code == 4 ){  // 申请失败
				num = 4
				mui.openWindow({
					url: "../my/apply_fail.html",
					extras: {
						num:num,
					},
					createNew: true
				});
			}
		}
	})
//	mui.openWindow({
//		url: "../my/user-upgrade.html",
//		createNew: true
//	});
});
//function userOninfo() {
//	mui.ajax(user_url + '/api/user/getuserInfo', {
//		dataType: 'json', //服务器返回json格式数据
//		type: 'post', //HTTP请求类型
//		timeout: 10000, //超时时间设置为10秒；
//		success: function(data) {
//			console.log(JSON.stringify(data))
//			if(data.code == 10000) {
//				description = data.resp.description; //实名认证驳回原因
//				if(data.resp.member_role=='普通会员'){
//					$(".my_user_btn").text('个人用户');
//					//商家入驻
//					document.getElementById("merchant_entry").addEventListener("tap", function() {
//						mui.openWindow({
//							url: "../my/user-upgrade.html",
//							createNew: true
//						});
//					});
//				}
//				else{
//					$(".my_user_btn").text(data.resp.member_role);
//					document.getElementById("merchant_entry").addEventListener("tap", function() {
//						mui.openWindow({
//							url: "../my/user_success.html",
//							createNew: true
//						});
//					});
//				}
//				
//			}
//		},
//		error: function(xhr, type, errorThrown) { //异常处理；
//			console.log(type);
//		}
//	});
//
//}

//页面进入执行方法
window.onload = function() {
	mui.plusReady(function() {
		self = plus.webview.currentWebview();
		plus.nativeUI.closeWaiting(); //关闭等待
		mask.close(); //关闭遮罩层
		userinfo();
		// userOninfo();
//		getInfo();
//		mui.ajax(order_url + '/order/myStatusOrderNum', { //我的各种订单状态接口
//			data: {
//				"sign": "string",
//				"user_id": user_id
//			},
//			dataType: 'json', //服务器返回json格式数据
//			type: 'post', //HTTP请求类型
//			timeout: 10000, //超时时间设置为10秒；
//			headers: {
//				'Content-Type': 'application/json'
//			},
//			success: function(data) {
//				if(data.code == 10000) {
//					var my_user_stay_payment_num = data.resp.status_one; //代付款
//					var my_user_stay_goods_num = data.resp.status_two; //待收货
//					var my_user_to_shipped = data.resp.status_shipments
//					if(my_user_stay_payment_num != 0) {
//						$("#my_user_stay_payment_num").text(my_user_stay_payment_num);
//					}
//					if(my_user_stay_goods_num != 0) {
//						$("#my_user_stay_goods_num").text(my_user_stay_goods_num);
//					}
//					if(my_user_to_shipped != 0) {
//						$("#my_user_to_shipped").text(my_user_to_shipped);
//					}
//				}
//			},
//			error: function(xhr, type, errorThrown) { //异常处理；
//				console.log(type);
//				var error_msg = JSON.parse(xhr.responseText)
//				mui.alert(error_msg.msg)
//			}
//		});
	});
}
////待付款
//document.getElementById("for_payment").addEventListener("tap", function() {
//	plus.nativeUI.showWaiting("加载中...");
//	mask.show(); //显示遮罩层
//	mui.openWindow({
//		url: "../my/for_payment.html",
//		id: "for_payment",
//		createNew: true,
//		waiting: {
//			autoShow: false
//		}
//	}, mask.close());
//});
////待发货
//document.getElementById("to_shipped").addEventListener("tap", function() {
//	plus.nativeUI.showWaiting("加载中...");
//	mask.show(); //显示遮罩层
//	mui.openWindow({
//		url: "../my/to_shipped.html",
//		id: "to_shipped",
//		createNew: true,
//		waiting: {
//			autoShow: false
//		}
//	}, mask.close());
//});
////待收货
//document.getElementById("for_goods").addEventListener("tap", function() {
//	plus.nativeUI.showWaiting("加载中...");
//	mask.show(); //显示遮罩层
//	mui.openWindow({
//		url: "../my/for_goods.html",
//		id: "for_goods",
//		createNew: true,
//		waiting: {
//			autoShow: false
//		}
//	}, mask.close());
//});
////已完成
//document.getElementById("Completed").addEventListener("tap", function() {
//	plus.nativeUI.showWaiting("加载中...");
//	mask.show(); //显示遮罩层
//	mui.openWindow({
//		url: "../my/has_been_completed.html",
//		id: "has_been_completed",
//		createNew: true,
//		waiting: {
//			autoShow: false
//		}
//	}, mask.close());
//});
//已取消
//document.getElementById("cancellation_order").addEventListener("tap", function() {
//	plus.nativeUI.showWaiting("加载中...");
//	mask.show(); //显示遮罩层
//	mui.openWindow({
//		url: "cancellation_order.html",
//		id: "cancellation_order",
//		createNew: true,
//		waiting: {
//			autoShow: false
//		}
//	}, mask.close());
//});

mui.back = function() {
	window.top.location.href = '../../index.html'
}
//二级按钮退出APP
var first = null;
mui.back = function() {
	//首次按键，提示‘再按一次退出应用’
	if(!first) {
		first = new Date().getTime();
		mui.toast('再按一次退出应用');
		setTimeout(function() {
			first = null;
		}, 1000);
	} else {
		if(new Date().getTime() - first < 1000) {
			plus.runtime.quit();
		}
	}
};
//代售
$(document).on("tap", ".daishou", function() {
	mui.openWindow({
		url: "daishou.html",
		id: 'daishou',
		createNew: true
	});
})